package com.weighttracker.android;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.bappi.custom.activities.CustomListActivity;
import com.bappi.utils.AppDetails;
import com.bappi.utils.Utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Tab5Activity1 extends CustomListActivity {

	private List<AppDetails> appDetails;
	private AppListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_tab_5_screen_1);

			new AsyncTask<String, String, String>() {
				private ProgressDialog progressDialog;

				protected void onPreExecute() {
					try {
						progressDialog = Utils.getProgressDialog(getParent());
						progressDialog.show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				protected String doInBackground(String... arg0) {
					try {
						appDetails = new ArrayList<AppDetails>();
						adapter = new AppListAdapter(getParent());
						JSONArray ja = Utils.getOtherAppsInfo(getParent());
						JSONObject jo;
						for (int i = 0; i < ja.length(); i++) {
							jo = ja.getJSONObject(i);
							appDetails.add(new AppDetails(
									jo.getString("title"), jo
											.getString("packaage"), Utils
											.getAppLogoId(getParent(),
													jo.getString("file"))));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {
						setListAdapter(adapter);
						progressDialog.dismiss();

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void onListItemClick(ListView l, View v, final int position,
			long id) {
		try {
			Intent marketIntent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("market://search?q=pname:"
							+ appDetails.get(position).getAppPackage()));
			startActivity(marketIntent);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class AppListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		private Context context;

		public AppListAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.app_list_content,
							null);
					holder = new ViewHolder();
					holder.mainContainer = convertView
							.findViewById(R.id.main_container);
					holder.title = (TextView) convertView
							.findViewById(R.id.app_title);
					holder.imageView = (ImageView) convertView
							.findViewById(R.id.app_icon_image);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				if (position == 0) {
					holder.mainContainer
							.setBackgroundResource(R.drawable.rounded_top_background_white);
				} else if (position == (getCount() - 1)) {
					holder.mainContainer
							.setBackgroundResource(R.drawable.rounded_bottom_background_white);
				} else {
					holder.mainContainer
							.setBackgroundResource(R.drawable.rect_background_white);
				}
				holder.imageView.setImageResource(appDetails.get(position)
						.getAppIconId());
				holder.title.setText(appDetails.get(position).getAppName());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			View mainContainer;
			ImageView imageView;
			TextView title;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public int getCount() {
			return appDetails.size();
		}

		public Object getItem(int position) {
			return appDetails.get(position);
		}
	}
}
